<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template;

interface ITriggers
{

  public function add(ITrigger $oTrigger);
  public function remove($key);
  public function hasTrigger();
/*
 public function hasFresh();
 public function runFresh($sTag);
 public function hasTag($sTag);


 */
}

class Triggers implements ITriggers, \Iterator
{
  public function add(ITrigger $oTrigger, $key = \NULL)
  {
    if (\is_null($key))
    {
      $this->aTriggers[] = $oTrigger;
    }
    else
    {
      $this->aTriggers[$key] = $oTrigger;
    }
  }

  public function remove($key)
  {
    unset($this->aTriggers[$key]);
  }

  public function hasTrigger()
  {
    if (empty($this->aTriggers))
    {
      return \FALSE;
    }
    return \TRUE;
  }

  // Iterator
  public function rewind()
  {
    return reset($this->aTriggers);
  }

  public function current()
  {
    return current($this->aTriggers);
  }

  public function key()
  {
    return key($this->aTriggers);
  }

  public function next()
  {
    return next($this->aTriggers);
  }

  public function valid()
  {
    return key($this->aTriggers) !== \NULL;
  }

  /** PRIVATE *************************************************************************************/

  private $aTriggers = array();
}